<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>班级管理</title>
<link rel="stylesheet" href="js/jquery-ui.css">

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript">
	function refreshTable(){
		$.post('banji',{method:'allClass'},function(data){
			var tbody=$('#tbody');
			tbody.empty();
			for(var i=0;i<data.length;i++){
				var bj=data[i];
				var tr=$('<tr/>');
				tr.append($('<td/>').html(bj.id));
				tr.append($('<td/>').html(bj.name));
				tr.append($('<td/>').html(bj.manager));
				tr.append($('<td/>').html('<a href="javascript:xg('+bj.id+')">修改</a>'));
				tr.append($('<td/>').html('<a href="javascript:del('+bj.id+')">删除</a>'));
				tbody.append(tr);				
			}
		});
	}
		function xg(id){
			var dialog=$('#dialog');
			dialog.load('banji',{method:'ajaxEdit',id:id},function(){
				dialog.dialog('open');
				
			})
		}
		function del(id){
			if(confirm("是否要删除这条记录？")){
				window.location="banji?method=delete&id="+id;
			}
		}
		$(function(){
			refreshTable();
			$('#dialog').dialog({
				autoOpen:false,
				title:'编辑',
				modal:true,
				width:400,
				buttons:{
					'保存':function(){
						var form=$(this).find('form');
						var fromDom=form[0];
						var name=fromDom.name.value;
						var manager=fromDom.manager.value;
						if(name.length==0){
							alert("班级名称必填");
							return;
						}
						if(manager.length==0){
							alert("班主任必填");
							return;
						}
						
						var params=form.serialize();
						$.post('banji?method=ajaxSave',params,function(data){
							refreshTable();
						});
						$(this).dialog('close');
					},
					'关闭':function(){
						$('#dialog').dialog('close');
					}
					
				}
			});
		});
		function fandeng() {
			window.location="login.jsp"
		}	
		function fanzhu() {
			window.location="main?method=main"
		}	
</script>

</head>
<body>
<div id="dialog" style="display: none;"></div>
	<div class="contrainer">
		<h3>班级管理</h3>
		<table border="1" >
			<thead>
				<tr>
					<th>序号</th>
					<th>班级名称</th>
					<th>班主任</th>
					<th colspan="2">操作 </th>
				</tr>
				
			</thead>
			<tbody id="tbody">			
			</tbody>
		</table>		
	</div>
	<div class="aaa">
	<button onclick="fanzhu()" class="btn">返回主页面</button>
	<button onclick="fandeng()" class="btn">返回登录页面</button>
	</div>
</body>
</html>